package com.ocom.accesservice.controller;


import com.ocom.accesservice.service.AreaInfoService;
import com.ocom.accesservice.service.GroupInfoService;
import com.ocom.accesservice.service.GroupPerService;
import com.ocom.accesservice.service.PersonnelInfoService;
import com.ocom.common.constant.AccessServiceUrl;
import com.ocom.common.entity.access.EntityMjGroupInfo;
import com.ocom.common.entity.access.EntityMjGroupPer;
import com.ocom.common.entity.human.EntityPersonnelInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.wx.UserAccessPageSetAccessRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.GroupListVo;
import com.ocom.common.vo.UserAccessListVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
public class WxAccessController {

    @Autowired
    GroupPerService groupPerService;

    @Autowired
    GroupInfoService groupInfoService;

    @Autowired
    AreaInfoService areaInfoService;

    @Autowired
    PersonnelInfoService personnelInfoService;

    @PostMapping(AccessServiceUrl.URL_getUserAccessListByArea)
    public Result getUserAccessListByArea(Long companyId,Long areaNo){
        List<UserAccessListVo> list=groupPerService.getUserAccessListByArea(companyId,areaNo,0L);
        return ResultUtil.success(list);
    }




    @PostMapping(AccessServiceUrl.URL_getGroupListByArea)
    public Result getGroupListByArea(Long companyId,Long areaNo){
        List<GroupListVo> list=groupInfoService.getGroupList(companyId,areaNo);
        return ResultUtil.success(list);

    }

    @PostMapping(AccessServiceUrl.URL_WxChangeAccess)
    public Result setAccess(@RequestBody  UserAccessPageSetAccessRequest request){
        log.info("access修改权限:【{}】",request);
        EntityPersonnelInfo entityPersonnelInfo = personnelInfoService.getPersonInfo(request.getCompanyId(),request.getUserId());
        if(entityPersonnelInfo == null || entityPersonnelInfo.getStatus()==1){
            groupPerService.removeUser(request.getCompanyId(),request.getUserId());
            return ResultUtil.error(ResultEnum.Err_3026);
        }

        EntityMjGroupPer entityMjGroupPer=groupPerService.changeAccess(request.getCompanyId(),request);
        EntityMjGroupInfo entityMjGroupInfo=groupInfoService.getGroupInfoById(request.getCompanyId(),request.getRoleI(),request.getAreaNo());
        String msg= "成功";
        if(entityMjGroupInfo!=null){
            msg="权限已修改为："+entityMjGroupInfo.getGroupName();
        }
        return ResultUtil.info(200,msg,entityMjGroupPer);
    }


    @PostMapping(AccessServiceUrl.URL_Removeuser)
    public Result Removeuser(Long companyId,Long pId){
        groupPerService.removeUser(companyId,pId);
        return ResultUtil.success();
    }




}
